package com.xhj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhj.config.TeacherQuery;
import com.xhj.entity.Course;
import com.xhj.entity.Teacher;
import com.xhj.service.CourseService;
import com.xhj.service.TeacherService;
import com.xhj.util.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "讲师管理")
@CrossOrigin    // 设置允许跨域
@RestController
@RequestMapping("/EduAdmin/edu/teacher/")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "所有讲师列表")
    @RequestMapping("list")
    public ResultEntity getTeacherList() {

        List<Teacher> teacherList = teacherService.list(null);
        return ResultEntity.successWithData().data("rows",teacherList);
    }

    @ApiOperation(value = "根据讲师Id删除讲师")
    @RequestMapping("/delete/{id}")
    public ResultEntity deleteById(@PathVariable("id") String id) {

        teacherService.removeById(id);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "分页讲师列表")
    @RequestMapping("/{page}/{limit}")
    public ResultEntity getPageList(@ApiParam(name = "page", value = "当前页码", required = true)
                                                    @PathVariable("page") Long page,
                                                    @ApiParam(name = "limit", value = "每页记录数", required = true)
                                                    @PathVariable("limit") Long limit,
                                                    @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                                                    @RequestBody(required = false) TeacherQuery teacherQuery) {

        Page<Teacher> pageParam = new Page<>(page, limit);

        IPage<Teacher> teacherPage = teacherService.pageList(pageParam, teacherQuery);
        return ResultEntity.successWithData().data("rows",teacherPage.getRecords())
                                             .data("total",teacherPage.getTotal())
                                             .data("size",teacherPage.getSize())
                                             .data("pages",teacherPage.getPages())
                                             .data("current",teacherPage.getCurrent());
    }

    @ApiOperation(value = "新增讲师")
    @RequestMapping("add")
    public ResultEntity addTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                                   @RequestBody Teacher teacher) {

        teacherService.save(teacher);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "修改讲师")
    @RequestMapping("/update")
    public ResultEntity updateTeacher(@RequestBody Teacher teacher) {

        teacherService.updateById(teacher);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据Id查询讲师")
    @RequestMapping("/get/{id}")
    public ResultEntity getTeacher(@ApiParam(name = "id", value = "讲师Id", required = true)
                                   @PathVariable("id") String id) {

        Teacher teacher = teacherService.getById(id);
        List<Course> courseList = courseService.getCourseByTeacherId(id);
        return ResultEntity.successWithData().data("teacher",teacher).data("courseList",courseList);
    }


}
